package eu.codlab.androidemu;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import eu.codlab.androidemu.gbcdroid.FileChooserAdapter;
import eu.codlab.androidemu.gbcdroid.ListListener;
import eu.codlab.androidemu.origbc.FileSelectedListener;
import eu.codlab.androidemu.utilities.Game;

abstract public class ActivitiesHelperSpecific  implements ListListener, FileFilter, IActivitiesHelper{
	protected Activity _activity;
	protected Game _type;
	protected FileSelectedListener _file_selected_listener;
	protected int _list_int;
	

	protected View click = null;
	
	protected final File sdcardDir = Environment.getExternalStorageDirectory();
	protected File currentDir;
	protected String[] filters;
	protected EditText pathEdit;

	final public void setClickView(View v){
		click = v;
		changeTo(currentDir);
	}

	abstract public String[] getFileFilter();

	final public void refresh(){
		changeTo(currentDir);
	}

	final public void sdcard(){
		changeTo(currentDir);
	}

	final public void parent(){
		changeTo(currentDir);
	}

	final public boolean accept(File file) {
		String name = file.getName();

		// Do not show hidden files
		if (name.startsWith(".") || file.isDirectory())
			return false;

		name = name.toLowerCase();

		for (String f : filters) {
			if(name.endsWith(f))
				return true;
		}
		return false;
	}

	final public File getDirectoryFromFile(String path) {
		File dir = new File(path);
		if (!dir.isDirectory()) {
			dir = dir.getParentFile();
			if (dir != null && !dir.isDirectory())
				dir = null;
		}
		return dir;
	}

	final public void changeTo(File dir) {
		if(click == null){
			return;
		}
		if(dir == null)
			return;
		ListView _l = (ListView)click.findViewById(_list_int);
		if(_l != null){
			File[] files = dir.listFiles(filters == null ? null : this);
			if (files == null)
				files = new File[0];

			currentDir = dir;
			if(pathEdit != null)
				pathEdit.setText(dir.getAbsolutePath());

			List<String> items = new ArrayList<String>(files.length);
			for (File f : files) {
				String name = f.getName();
				if (f.isDirectory())
					name += '/';
				items.add(name);
			}

			Collections.sort(items, String.CASE_INSENSITIVE_ORDER);
			//if(!"/".equals(currentDir))
			//	items.add(0, "..");
			_l.setAdapter(new FileChooserAdapter(_activity, this, (ArrayList<String>) items, currentDir));
		}
		Log.d("Changing to",currentDir != null ? currentDir.getAbsolutePath() : "--");

	}


	@Override
	final public boolean onKey(View v, int keyCode, KeyEvent event) {
		return false;
	}


	//ListManagement
	final public void onListItemClick(String name, View v, int position, long id) {
		File f = currentDir;
		f = new File(currentDir, name);


		if(!f.isDirectory())
			_file_selected_listener.onFileSelected(_type,Uri.fromFile(f));
	}


}
